$().ready(function () {
    //弹幕初始化
    height = $("#playercontainer").height();
    width = $("#playercontainer").width();
    left = $('#playercontainer').css('left');
    top = $('#playercontainer').css('top');
    $("#danmu").danmu({
        left: left,    //区域的起始位置x坐标
        top: top,  //区域的起始位置y坐标
        height: height, //区域的高度
        width: width, //区域的宽度
        zindex: 100, //div的css样式zindex
        speed: 20000, //弹幕速度，飞过区域的毫秒数
        sumtime: 900, //弹幕运行总时间
        danmuss: {}, //danmuss对象，运行时的弹幕内容
        default_font_color: "#FFFFFF", //弹幕默认字体颜色
        font_size_small: 16, //小号弹幕的字体大小,注意此属性值只能是整数
        font_size_big: 24, //大号弹幕的字体大小
        opacity: "0.9", //弹幕默认透明度
        top_botton_danmu_time: 6000 //顶端底端弹幕持续时间
    });
    player = cyberplayer("playercontainer").setup({
        width :1200,
        height : 600,
        backcolor : "#FFFFFF",
        stretching : "uniform",
        file : "/public/static/2017-10-10-2112-12.flv",
        ak : "9e2889da373d4eb188a82365b2f2c918",
        autoStart : true,
        repeat : false,
        volume : 100,
        controls : "over"
    });
    $('#playercontainer').danmu('danmu_start');
    //连接websocket
    ws = new WebSocket("ws://live.com:2000");
    //打开弹幕发送按钮
    $('.danmu_start').removeClass("layui-btn-disabled");
    //发送用户信息后台分配信息
    param = {};
    param.access_token = $.cookie('access_token');
    param.id = GetQueryString('id');
    ws.onopen = function () {
        ws.send(JSON.stringify(param));
    };
    ws.onmessage = function (e) {
        console.log(e);
        if (e.data == 'close') {
            ws.close();
        } else {
            var data = JSON.parse(e.data)
            var html = ' <div class="admin-group">' +
                '            <img class="admin-img" src="img/aa.jpg"/>' +
                '            <div class="admin-msg">' +
                '                <i class="triangle-admin"></i>' +
                '                <span class="admin-reply">' + data.information + '</span>' +
                '            </div>' +
                '        </div>'
            $('.mobile-page').append(html);
            info = {
                "text": data.information,
                "color": "red",
                "size": "10",
                "position": "0",
                "time": $('#danmu').data("nowtime") + 5
            };
            $('#danmu').danmu("add_danmu", info);
        }
    };
    //弹幕开始
    $('.danmu_start').click(function () {
        info = {
            "text": $('.danmu').val(),
            "color": "red",
            "size": "10",
            "position": "0",
            "time": $('#danmu').data("nowtime") + 5,
            "isnew": ""
        };
        $('#danmu').danmu("add_danmu", info);
        param = {};
        param.id = GetQueryString('id');
        param.information = $('.danmu').val();
        param.time = $('#danmu').data("nowtime");
        param = JSON.stringify(param);
        ws.send(param);
        var html = '<div class="user-group">' +
            '            <div class="user-msg">' +
            '                <span class="user-reply">' + $('.danmu').val() + '</span>' +
            '                <i class="triangle-user"></i>' +
            '            </div>' +
            '            <img class="user-img" src="img/cc.jpg"/>' +
            '        </div>'
        $('.mobile-page').append(html);
    })

})

function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}